package com.cuit.service.impl;

import com.cuit.mapper.UserMapper;
import com.cuit.pojo.User;
import com.cuit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> findAll(){
        return userMapper.findAll();
    }
    public User findById(int id){
        return userMapper.findById(id);
    }
    public int deleteById(int id){
        return userMapper.deleteById(id);
    }
    public int login(String username, String password) {
        User user = userMapper.findByName(username);
        if(password.equals(user.getPassword()))
            return user.getId();
        else
            return 0;
    }
    public int register(String username, String password, MultipartFile image) throws IOException {
        String uuid = UUID.randomUUID().toString();
        String FileName = image.getOriginalFilename();
        int index =FileName.lastIndexOf(".");
        String extname = FileName.substring(index);
        String HeadName = uuid+extname;
        image.transferTo(new File("C:\\Users\\DELL\\Desktop\\blog\\blog\\src\\assets\\"+HeadName));
        User user = new User();
        user.setHead(HeadName);
        user.setName(username);
        user.setPassword(password);
        int i = userMapper.insertUser(user);
        return i;
    }
}
